package org.javastudy.tang.lambda.stream;

import org.javastudy.tang.vo.Employee;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p><em>Created on 2022/5/10 9:51 下午</em></p>
 *
 * @author fuzhongtang
 * @since 1.0.1
 */
public class List_stream_Collectors {
    public static void main(String[] args) {

        // 获取初始化员工列表集合
        List<Employee> employees = initList();

        // 1. 获取所有的员工id集合
        List<Integer> employeeList = employees.stream().map(Employee::getId).collect(Collectors.toList());


        // 2. 获取员工id与员工信息的Map集合
        Map<Integer, Employee> idEmployeeMap = employees.stream().collect(
                Collectors.toMap(Employee::getId, Function.identity()));


        // 3. 员工id与员工姓名一一对应的集合
        Map<Integer, String> idNameMap = employees.stream().collect(
                Collectors.toMap(Employee::getId, Employee::getName));


        // 4. 员工按部门分组
        Map<Integer, List<Employee>> departGroupMap = employees.stream().collect(Collectors.groupingBy(Employee::getDepartId));


        // 5. 员工按部门分组,姓名集合
        Map<Integer, List<String>> departNamesMap =
                employees.stream().collect(Collectors.groupingBy(Employee::getDepartId,
                        Collectors.mapping(Employee::getName, Collectors.toList()))
                );


    }

    private static List<Employee> initList() {

        List<Employee> employees = new ArrayList<>();

        employees.add(new Employee(1, "apple", 22, 1));

        employees.add(new Employee(2, "banana", 33, 2));

        employees.add(new Employee(3, "mango", 22, 3));

        employees.add(new Employee(4, "pear", 41, 1));

        employees.add(new Employee(5, "grape", 54, 3));

        employees.add(new Employee(6, "orange", 68, 5));

        employees.add(new Employee(7, "peach", 29, 2));

        employees.add(new Employee(8, "raisin", 37, 1));

        return employees;

    }
}
